package com.mh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mh.dao.UsersDao;
import com.mh.dto.R;
import com.mh.pojo.AuctionProduct;
import com.mh.pojo.Users;
import com.mh.service.UsersService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * (Users)表服务实现类
 *
 * @author zmh
 * @since 2023-07-27 16:02:38
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersDao, Users> implements UsersService {

    @Autowired
    private UsersDao usersDao;


    /**
     * 分页查询
     *
     * @param page 查询页数
     * @param size 一页显示条数
     * @return ·
     */
    @Override
    public R<Page<Users>> getAllByPage(int page, int size) {
        Page<Users> usersPage = new Page<>(page, size);
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        //TODO 查询条件定制

        //执行查询
        usersDao.selectPage(usersPage,queryWrapper);
        return R.success(usersPage);
    }

    /**
     * 修改用户的竞品发布数量
     * @param uid UID
     * @return ·
     */
    @Override
    public R<String> updatePublishNumber(String uid) {
        // 根据用户ID给发布竞品数量自增
        int i = usersDao.incrementPublishNumber(uid, 1);
        return i == 1? R.success("修改成功"): R.error("修改错误");
    }

    /**
     * 充值
     * @param price 充值价格
     * @return ·
     */
    @Override
    public R<String> updateBalance(String userId, Double price) {
        LambdaUpdateWrapper<Users> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Users::getUid, userId);
        updateWrapper.set(Users::getBalance, price);
        usersDao.update(null, updateWrapper);
        return R.success("充值成功");
    }

    /**
     * 根据用户名查询用户信息
     * @param userName 用户名
     * @return ·
     */
    @Override
    public R<Users> getUserByName(String userName) {
        // 条件构造
        LambdaUpdateWrapper<Users> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(Users::getName, userName);
        Users user = usersDao.selectOne(queryWrapper);
        if(user == null){
             return R.error("无此用户");
        }
        return R.success(user);
    }
}

